//#define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression) // for older version of sdl

#include "SocketMessage.h"
#include <assert.h>

SocketMessage::SocketMessage()
{

}

void SocketMessage::Clear()
{

}

const unsigned char* SocketMessage::GetData() const
{
	return m_chars.data();
}

const int SocketMessage::GetLength() const
{
	return m_chars.size();
}

bool SocketMessage::ReadFloat(float* result)
{
	int diff = m_chars.size() - m_dataIndex;
	if (diff < sizeof(float))
	{
		assert(0);
		return false;
	}
	float* f = (float*)(&(m_chars[m_dataIndex]));
	m_dataIndex += sizeof(float);
	*result = SDLNet_Read32(f);
	return true;
}

bool SocketMessage::ReadInt(int* result)
{
	int diff = m_chars.size() - m_dataIndex;
	if (diff < sizeof(int))
	{
		assert(0);
		return false;
	}
	int* i = (int*)(&(m_chars[m_dataIndex]));
	m_dataIndex += sizeof(int);
	*result = SDLNet_Read32(i);

	return true;
}

bool SocketMessage::ReadString(std::string* result)
{
	int diff = m_chars.size() - m_dataIndex;
	if (diff < sizeof(&result))
	{
		assert(0);
		return false;
	}

	std::string* i = (std::string*)(&(m_chars[m_dataIndex]));
	m_dataIndex += sizeof(unsigned char*) * result->size();
	*result = SDLNet_Read32(i);

	return true;
}

void SocketMessage::WriteFloat(float data)
{
	SDLNet_Write32(data, &data);
	unsigned char* chars = (unsigned char*)(&data);
	m_chars.insert(m_chars.end(), chars, chars + sizeof(float));
}

void SocketMessage::WriteInt(int data)
{
	SDLNet_Write32(data, &data);
	unsigned char* chars = (unsigned char*)(&data);
	m_chars.insert(m_chars.end(), chars, chars + sizeof(int));
}

void SocketMessage::WriteString(const std::string& data)
{
	const char* d = data.c_str();

	SDLNet_Write32((int)d, &d);
	unsigned char* chars = (unsigned char*)(&data);
	m_chars.insert(m_chars.end(), chars, chars + (sizeof(unsigned char*) * data.size()));
}